package main

import "fmt"

func main() {
	num := 16
	fmt.Println(isPerfectSquare(num))
}

//#link: https://leetcode-cn.com/problems/valid-perfect-square/
/**
 * @Description:  二分查找
 */
func isPerfectSquare(num int) bool {
	left, right := 0, num
	// 因为搜索区间的边界始终没检查过 所以当左右边界相等时也要进行判断
	for left <= right {
		mid := (left + right) / 2
		square := mid * mid
		if square < num {
			left = mid + 1
		} else if square > num {
			right = mid - 1
		} else {
			return true
		}
	}
	return false
}

/**
 * @Description: 暴力破解
 */
func isPerfectSquare2(num int) bool {
	for x := 1; x*x <= num; x++ {
		if x*x == num {
			return true
		}
	}
	return false
}
